<?php 
	include 'DBManager.class.php';
	
	$filterAdult = "AND genre NOT LIKE '%adult%'";
	
	$toAppendQuery = '';
	
   if ( isset($_GET['currentPage']) ){
   		$start = ($_GET['currentPage']-1) * 4;
		$thisPage = $_GET['currentPage'];
   }
	else {
		$start = 0;
		$thisPage = 1;
	}
		
	if ( isset($_GET['genre']) )
   		$genre = $_GET['genre'];
	else 
		$genre = '';
		
	if ($genre == 'adult')
		$filterAdult = "";
		
	if ( isset($_GET['year']) )
   		$year = ' AND year = '.$_GET['year'];
	else 
		$year = '';
		
	
	$toAppendEnd = '';
	if ( isset($_GET['genre']) )
		$toAppendEnd = '&genre='.$genre;				
	if ( isset($_GET['year']) )
		$toAppendEnd = '&year='.$_GET['year'];
	
	$raccomandatebwwm = "<a href='table.php?x=0".$toAppendEnd."' style='color:#8D8D8D;'>Recommended by BWWM</a>";
	$directorASC = "<a href='table.php?directorASC=true".$toAppendEnd."' style='color:#8D8D8D;'>Director (A-Z)</a>";
	$directorDESC = "<a href='table.php?directorDESC=true".$toAppendEnd."' style='color:#8D8D8D;'>Director (Z-A)</a>";
	
	$releasedASC = "<a href='table.php?releasedASC=true".$toAppendEnd."' style='color:#8D8D8D;'>Oldest to newest</a>";
	
	$releasedDESC = "<a href='table.php?releasedDESC=true".$toAppendEnd."' style='color:#8D8D8D;'>Newest to Oldest</a>";
	
	$titleASC = "<a href='table.php?titleASC=true".$toAppendEnd."' style='color:#8D8D8D;'>Tilte (A-Z)</a>";
	
	
	if ( isset($_GET['directorASC'] ) ){
		$toAppendQuery = 'director ASC, ';
		$directorASC = "<b>".$directorASC."</b>";
	}
	else if ( isset($_GET['directorDESC'] ) ){
		$toAppendQuery = 'director DESC, ';
		$directorDESC = "<b>".$directorDESC."</b>";
	}
	else if ( isset($_GET['titleASC'] ) ){
		$toAppendQuery = 'title ASC, ';	
		$titleASC = "<b>".$titleASC."</b>";
	}
	else if ( isset($_GET['releasedASC'] ) ){
		$toAppendQuery = 'released ASC, ';
		$releasedASC = "<b>".$releasedASC."</b>";
	}
	else if ( isset($_GET['releasedDESC'] ) ){
		$toAppendQuery = 'released DESC, ';
		$releasedDESC = "<b>".$releasedDESC."</b>";
	}
	else 
		$raccomandatebwwm = "<b>".$raccomandatebwwm."</b>";
	

$ordina = '<tr><td style="border:none">'.$raccomandatebwwm.'</td><td style="border:none">'.$releasedDESC.'</td><td style="border:none">'.$releasedASC.'</td></tr>
                                        <tr><td style="border:none">'.$titleASC.'</td><td style="border:none">'.$directorASC.'</td><td style="border:none">'.$directorDESC.'</td></tr>';
			
			$db = new DBManager ( 'localhost', 'root', '', 'iumus' );
			
			$query="SELECT COUNT(*) FROM movie WHERE id NOT IN (SELECT id FROM movie WHERE id <> id_origin) AND title NOT LIKE '%episode%' ".$filterAdult." AND runtime LIKE '%hr%' AND genre LIKE ";
			
			$g = explode ( '-', $genre );
			if ( count ( $g ) > 1 )
				$query .= "'%".$g[0]."%' AND genre LIKE '%".$g[1]."%' ";
			else $query .= "'%".$genre."%' ";
			
			$query .= "$year ORDER BY ".$toAppendQuery." votes DESC, rating DESC";
			$result=$db->executeQuery ($query);
			
			$row = mysql_fetch_array ( $result );
			
			$numfilm = $row[0]/4;
			$numfilm = floor($numfilm)+1;
			
			$query="SELECT * FROM movie WHERE id NOT IN (SELECT id FROM movie WHERE id <> id_origin) AND title NOT LIKE '%episode%' ".$filterAdult." AND runtime LIKE '%hr%' AND genre LIKE ";
			
			$g = explode ( '-', $genre );
			if ( count ( $g ) > 1 )
				$query .= "'%".$g[0]."%' AND genre LIKE '%".$g[1]."%' ";
			else $query .= "'%".$genre."%' ";
						
			$query .= "$year ORDER BY $toAppendQuery votes DESC, rating DESC  LIMIT $start, 4 ";
			
			$result=$db->executeQuery ($query);
			
			echo '<table style="height:100%;color:#8D8D8D; text-align:justify; font-family:Tahoma, Geneva, sans-serif; font-size:11px;width:100%;" id="tabellaLista" >';
			
			echo '<tr><td colspan="5"><table style="color:#8D8D8D; text-align:justify; font-family:Tahoma, Geneva, sans-serif; font-size:11px; width:100%; border:solid 1px" >
                                    <tr><th colspan="3" style=" color:#FF5000; font-weight:bold;background:#eee; ">ORDINA I RISULTATI</th></tr>'.$ordina.'
                                        
                                    </table></td></tr>';
			
			$countFilm = 0;
			while ( $row = mysql_fetch_array ( $result )  ) {
					
					if($row['cover']=='N/A')
            			$pathImage='../../wp-content/themes/theme1273/images/noCover.png';
       				else
        				$pathImage = $row['cover'];
						
        			if($row['plot']=='N/A')
            			$plot='Plot not yet released<br>';
        			else
        				$plot = substr($row['plot'],0,100);
						
					$actors = $row["actors"];
					$title = $row["title"];
					
					$content = '';
					$content .= '<tbody>';
					$content .= '<tr style="cursor:pointer;" onClick="self.parent.parent.location.href = \'../../infoFilm.php?id='.$row["id"].'\'">';
					$content .= '<td rowspan="5"> <img src="'.$pathImage.'"  width="75" height="105" /> </td> <td style="text-transform:uppercase;)"> <span style="font-size:13px; color:#FF5000; font-weight:bold">'.$title.' - ('.$row["year"].')</span> </td>';
					$content .= '<td>&nbsp;</td> <td style="text-align:right">'.$row["rating"].'/10</td> <td rowspan="5">&nbsp;</td>';
					$content .= '</tr>';
					$content .= '<tr><td colspan="2"><span style="font-size:12px; color:#FFF; font-weight:bold">Director: </span>'.$row["director"].'</td> </tr>';
					$content .= '<tr><td colspan="3"><hr />'.$row["runtime"].'  -  '.$row["genre"].' <br /> <hr /> </td> </tr>';
					$content .= '<tr><td colspan="3">'.$plot.'...</td></tr>';
					$content .= '<tr><td colspan="3"><span style="font-size:12px; color:#FFF; font-weight:bold">Stars: </span>'.$actors.'</td></tr>';
					$content .= '</tbody>';
					
					echo $content;
					
					$countFilm++;
				
			}
			
			for (;$countFilm<4;$countFilm++){
					$content = '';
					$content .= '<tbody style="visibility:hidden;">';
					$content .= '<tr style="cursor:pointer;" onClick="self.parent.parent.location.href = \'../../infoFilm.php?id='.$row["id"].'\'">';
					$content .= '<td rowspan="5"> <img src="'.$pathImage.'"  width="75" height="105" /> </td> <td style="text-transform:uppercase;)"> <span style="font-size:12px; color:#FF5000; font-weight:bold">'.$title.' - ('.$row["year"].')</span> </td>';
					$content .= '<td>&nbsp;</td> <td style="text-align:right">'.$row["rating"].'/10</td> <td rowspan="5">&nbsp;</td>';
					$content .= '</tr>';
					$content .= '<tr><td colspan="2"><span style="font-size:12px; color:#FFF; font-weight:bold">Director: </span>'.$row["director"].'</td> </tr>';
					$content .= '<tr><td colspan="3"><hr />'.$row["runtime"].'  -  '.$row["genre"].' <br /> <hr /> </td> </tr>';
					$content .= '<tr><td colspan="3">'.$plot.'...</td></tr>';
					$content .= '<tr><td colspan="3"><span style="font-size:12px; color:#FFF; font-weight:bold">Stars: </span>'.$actors.'</td></tr>';
					$content .= '</tbody>';
					
					echo $content;
			}
			
			$toAppendNB ='';
			if ( isset($_GET['directorASC'] ) ){
				$toAppendNB = 'directorASC=true';
				
			}
			else if ( isset($_GET['directorDESC'] ) ){
				$toAppendNB = 'directorDESC=true';
				
			}
			else if ( isset($_GET['titleASC'] ) ){
				$toAppendNB = 'titleASC=true';
				
			}
			else if ( isset($_GET['releasedASC'] ) ){
				$toAppendNB = 'releasedASC=true';
				
			}
			else if ( isset($_GET['releasedDESC'] ) ){
				$toAppendNB = 'releasedDESC=true';
			}
			
		
			
			$page = '';
			if($thisPage < 8 ){
				for ($i=1; $i<10; $i++ ){
					if ($i != $thisPage)
						$page .= '<a style="color:#FFF" href="table.php?'.$toAppendNB.'&currentPage='.($i).$toAppendEnd.'">'.$i.'</a> - ';
					else{
						$page .= ''.$i.' - ';
					}
				}
				$page .='... -';
				$page .= '<a style="color:#FFF" href="table.php?'.$toAppendNB.'&currentPage='.$numfilm.$toAppendEnd.'">'.$numfilm.'</a> ';

			}
			else if ( $thisPage >= 8 && $thisPage <= ($numfilm-8) ){
				$page = '<a style="color:#FFF" href="table.php?'.$toAppendNB.'&currentPage=1'.$toAppendEnd.'">1</a> - ';
				$page .='... -';
				for ($i= ($thisPage -4); $i<($thisPage + 5); $i++ ){
					if ($i != $thisPage)
						$page .= '<a style="color:#FFF" href="table.php?'.$toAppendNB.'&currentPage='.($i).$toAppendEnd.'">'.$i.'</a> - ';
					else{
						$page .= ''.$i.' - ';
					}
				}
				$page .='... -';
				$page .= '<a style="color:#FFF" href="table.php?'.$toAppendNB.'&currentPage='.$numfilm.$toAppendEnd.'">'.$numfilm.'</a> ';
			}
			else{
				$page = '<a style="color:#FFF" href="table.php?'.$toAppendNB.'&currentPage=1'.$toAppendEnd.'">1</a> - ';
				$page .='... -';
				for ($i=$thisPage-5; $i<$numfilm+1; $i++ ){
					if ($i != $thisPage){
						$page .= '<a style="color:#FFF" href="table.php?'.$toAppendNB.'&currentPage='.($i).$toAppendEnd.'">'.$i.'</a>';
					}
					else{
						$page .= ''.$i;
					}
					if($i != $numfilm)
						$page .= ' - ';
				}
			}
			
	
			
			$nextConent = '<img src="../../wp-content/themes/theme1273/images/prettyPhoto/dark_rounded/btnNext.png"  />';
			$backConent = '<img src="../../wp-content/themes/theme1273/images/prettyPhoto/dark_rounded/btnPrevious.png"  />';
			
			if( $start == 0 ){
				$next = '<a style="color:#FFF;" href="table.php?'.$toAppendNB.'&currentPage='.($thisPage+1).$toAppendEnd.'">'.$nextConent.'</a> ';
				$back = ''.$backConent.'';
			}
			else if ($thisPage == $numfilm ){
				$next = ''.$nextConent.'';
				$back = '<a style="color:#FFF;" href="table.php?'.$toAppendNB.'&currentPage='.($thisPage-1).$toAppendEnd.'">'.$backConent.'</a> ';
			}
			else{
				$next = '<a style="color:#FFF" href="table.php?'.$toAppendNB.'&currentPage='.($thisPage+1).$toAppendEnd.'">'.$nextConent.'</a> ';
				$back = '<a style="color:#FFF" href="table.php?'.$toAppendNB.'&currentPage='.($thisPage-1).$toAppendEnd.'">'.$backConent.'</a> ';
			}
			
			
			
			echo '<tfoot><tr><td colspan="5">
				<table width="100%" style="color:#8D8D8D; text-align:justify; font-family:Tahoma, Geneva, sans-serif; font-size:11px;">         
					<tr> <td colspan="6"> <hr> </td></tr>
                   	<tr> 
						<td colspan="3">
							'.$back.'		
						</td>
						<td colspan="2" style="text-align:center">
								'.$page.'
						</td>
						<td style="text-align:right"> 
							'.$next.'
						</td>
					</tr>
				</table></td></tr></tfoot>';
			echo '</table>';
			
			echo ' <script type="text/javascript"> document.parent.variabileCondivisa =  document.getElementById ("tabellaLista").style.height.value </script>';
?>
